<template>
  <div class="member-footmark">
    <div class="xtx-center-head">
      <h4 class="title fl">我的足迹</h4>
      <a href="javascript:;" class="add fr" @click="deleteHistory('all')" v-if="today.items.length || yesterday.items.length || longago.items.length">清除全部</a>
    </div>
    <!-- 足迹列表数据 -->
    <div class="footprint-list" v-if="today.items.length || yesterday.items.length || longago.items.length">
      <!-- <FootmarkListItem :footmark="footmarkList"/> -->
      <div class="item">
    <div class="line active"></div>
      <div class="goods">
        <p class="time">
          <span>今天</span>
          <a href="javascript:;" v-if="today.items.length" @click="deleteHistory('today')">删除</a>
        </p>
        <ul>
          <li v-for="goods in today.items"  :key="goods.id">
            <i class="close iconfont icon-guanbi" @click="deleteHistory('today', goods.id)"></i>
            <a href="javascript:;">
              <img src="https://yanxuan-item.nosdn.127.net/f7a4f643e245d03771d6f12c94e71214.png" alt="">
              <p class="name ellipsis">{{goods.name}}</p>
              <p class="desc">{{goods.desc}}</p>
              <p class="price">&yen;{{goods.price}}</p>
            </a>
          </li>
        </ul>
      </div>
    </div>
    <div class="item">
      <div class="line"></div>
      <div class="goods">
        <p class="time">
          <span>昨天</span>
          <a href="javascript:;" v-if="yesterday.items.length" @click="deleteHistory('yesterday')">删除</a>
        </p>
        <ul>
          <li v-for="goods in yesterday.items" :key="goods.id">
            <i class="close iconfont icon-guanbi"  @click="deleteHistory('yesterday', goods.id)"></i>
            <a href="javascript:;">
              <img src="https://yanxuan-item.nosdn.127.net/f7a4f643e245d03771d6f12c94e71214.png" alt="">
              <p class="name ellipsis">{{goods.name}}</p>
              <p class="desc">{{goods.desc}}</p>
              <p class="price">&yen;{{goods.price}}</p>
            </a>
          </li>
        </ul>
      </div>
    </div>
    <div class="item">
      <div class="line"></div>
      <div class="goods">
        <p class="time">
          <span>更早之前</span>
          <a href="javascript:;" v-if="longago.items.length > 0" @click="deleteHistory('longago')">删除</a>
        </p>
        <ul>
          <li v-for="goods in longago.items" :key="goods.id">
            <i class="close iconfont icon-guanbi" @click="deleteHistory('longago', goods.id)"></i>
            <a href="javascript:;">
              <img src="https://yanxuan-item.nosdn.127.net/f7a4f643e245d03771d6f12c94e71214.png" alt="">
              <p class="name ellipsis">{{goods.name}}</p>
              <p class="desc">{{goods.desc}}</p>
              <p class="price">&yen;{{goods.price}}</p>
            </a>
          </li>
        </ul>
      </div>
    </div>
     <!-- 无限加载组件 -->
    <XtxInfiniteLoading :loading="loading" :finished="finished" @infinite="getItems" />
    </div>
    <div class="none" v-else>抱歉，当前无浏览足迹记录</div>
  </div>
</template>

<script>
import { onMounted, reactive, ref } from 'vue'
import { findFootMarks, deleteFootMarks } from '@/api/member'
import dayjs from 'dayjs'
import Confirm from '@/components/library/confirm'
import Message from '@/components/library/message'
export default {
  name: 'FootmarksPage',
  setup () {
    // 查询参数
    const reqParams = reactive({
      page: 0,
      pageSize: 12
    })
    const today = reactive({
      name: '今天',
      items: []
    })
    const yesterday = reactive({
      name: '昨天',
      items: []
    })
    const longago = reactive({
      name: '更早以前',
      items: []
    })
    const dateMap = reactive({
      all: 4,
      today: 1,
      yesterday: 2,
      longago: 3
    })
    onMounted(() => {
      getItems()
    })
    // 加载中
    const loading = ref(false)
    // 是否加载完毕
    const finished = ref(false)
    // 获取足迹数据
    const getItems = () => {
      loading.value = true
      reqParams.page++
      findFootMarks(reqParams).then(data => {
        if (data.result.items.length) {
          const todayDate = dayjs().hour(0).minute(0).millisecond(0)
          const yesterdayDate = todayDate.subtract(1, 'day')
          data.result.items.forEach(item => {
            if (dayjs(item.browserTime).isBefore(todayDate)) {
              if (dayjs(item.browserTime).isBefore(yesterdayDate)) {
                longago.items.push(item)
              } else {
                yesterday.items.push(item)
              }
            } else {
              today.items.push(item)
            }
          })
        } else {
          // 没有取到数据，表示加载完成
          finished.value = true
        }
        loading.value = false
      })
    }
    // 删除浏览记录
    const deleteHistory = (type, id) => {
      Confirm({ text: '您确认要删除这些足迹吗？' }).then(() => {
        const data = ref({})
        if (dateMap[type] && id === undefined) {
          data.value.dateType = dateMap[type]
        } else {
          data.value.ids = [id]
        }
        deleteFootMarks(data.value).then(data => {
          Message({ type: 'success', text: '删除成功' })
        })
      }).catch(e => {})
    }
    return { reqParams, getItems, dateMap, today, yesterday, longago, loading, finished, deleteHistory }
  }
}
</script>

<style lang="less" scoped>
.member-footmark {
  height: 100%;
  background: #fff;
  .xtx-center-head {
    line-height: 70px;
    margin: 0 25px;
    height: 70px;
    border-bottom: 1px solid #f5f5f5;
    .title {
      font-size: 16px;
      font-weight: 400;
    }
    .add {
      color: #27ba9b;
    }
  }
  .footprint-list {
    padding: 40px 25px;
    .item {
    display: flex;
    .line {
      width: 27px;
      border-right: 2px solid rgb(245, 245, 245);
      margin-right: 25px;
      position: relative;
      &::before {
        content: "";
        position: absolute;
        top: 0;
        right: 0;
        transform: translate(7px);
        border-radius: 50%;
        width: 12px;
        height: 12px;
        background: rgb(228, 228, 228);
      }
    }
    .active::before {
      background: rgb(39, 186, 155);
      z-index: 1;
    }
    .active::after {
      content: "";
      position: absolute;
      top: 0;
      right: 0;
      transform: translate(11px, -4px);
      border-radius: 50%;
      width: 20px;
      height: 20px;
      background: rgb(227, 249, 244);
    }
    .goods {
      flex: 1 1 0%;
    .time {
      margin-top: -5px;
      span {
        font-size: 16px;
        color: rgb(153, 153, 153)
      }
      a {
        color: #cf4444!important;
        margin-left: 10px;
      }
    }
    ul {
      display: flex;
      flex-wrap: wrap;
      padding-top: 25px;
      li {
        width: 278px;
        height: 370px;
        padding-top: 25px;
        text-align: center;
        margin-bottom: 25px;
        margin-right: 30px;
        position: relative;
        &:nth-child(3n) {
          margin-right: 0;
        }
        img {
          width: 192px;
          height: 192px;
        }
        .close {
          position: absolute;
          right: 0px;
          top: 0px;
          width: 30px;
          height: 30px;
          background: rgb(228, 228, 228);
          color: rgb(255, 255, 255);
          text-align: center;
          line-height: 30px;
          display: none;
          cursor: pointer;
        }
        &:hover {
          transition: all .5s;
          transform: translate3d(0,-3px,0);
          box-shadow: 0 3px 8px rgba(0,0,0,0.2);
          background: #f0f9f4;
          i {
            display: block;
          }
        }
        .name {
         font-size: 16px;
          padding-top: 15px;
        }
        .desc {
          color: rgb(153, 153, 153);
          padding-top: 18px;
        }
        .price {
          font-size: 18px;
          color: rgb(207, 68, 68);
          padding-top: 24px;
        }
      }
    }
    }
  }
  }
  .none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
}

</style>
